<template>
  <div id="login">
    <el-row>
      <el-col :span="24">
        <div style="height: 150px;"></div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :offset="8" :span="8">
        <el-card class="box-card" :class="{ 'scale-in': showCard }">
          <div slot="header" class="clearfix">
            <span>后台用户登录</span>
          </div>
          <div>
            <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px" class="demo-userForm">
              <el-form-item label="用户名：" prop="loginName">
                <el-input v-model="userForm.loginName" placeholder="请输入用户名"></el-input>
              </el-form-item>
              <el-form-item label="密码：" prop="password">
                <el-input type="password" v-model="userForm.password" placeholder="请输入密码"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm('userForm')" class="login-button">登录</el-button>
                <el-button @click="resetForm('userForm')" class="reset-button">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userForm: {
        loginName: '',
        password: ''
      },
      rules: {
        loginName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ]
      },
      showCard: false
    };
  },
  mounted() {
    this.showCard = true;
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('http://localhost:8090/sys-users/login', {
            loginName: this.userForm.loginName,
            password: this.userForm.password
          })
              .then(response => {
                if (response.data.success) {
                  this.$store.commit('initSysUser', response.data.user);
                  this.$router.push('/backend');
                } else {
                  this.$message.error(response.data.message);
                }
              })
              .catch(error => {
                console.error('登录请求出错:', error);
                this.$message.error('登录请求出错，请稍后重试');
              });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 5px;
  background-color: rgba(255, 255, 255, 0.9);
  animation: fadeInUp 0.5s ease;
}

.scale-in {
  transform: scale(1);
  animation: scaleIn 0.3s ease;
}

@keyframes scaleIn {
  from {
    transform: scale(0.8);
  }
  to {
    transform: scale(1);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.demo-userForm {
  padding: 20px;
}

.login-button {
  margin-right: 10px;
  background-color: #409eff;
  border-color: #409eff;
  transition: background-color 0.3s ease;
}

.login-button:hover {
  background-color: #66b1ff;
}

.reset-button {
  background-color: #f4f4f4;
  border-color: #dcdfe6;
  color: #606266;
  transition: background-color 0.3s ease;
}

.reset-button:hover {
  background-color: #e4e7ed;
}
#login{
  height: 940px;
  background-image: url('@/assets/images/cc.jpg'); /* 确保路径正确 */
  background-size: cover; /* 背景图覆盖整个窗口 */
  background-position: center; /* 背景图居中 */
  background-attachment: fixed; /* 背景图固定，不随滚动条滚动 */
  margin: 0;
  padding: 0;
}
/* 全局样式：设置页面背景图 */
body {
  background-image: url('@/assets/images/cc.jpg'); /* 确保路径正确 */
  background-size: cover; /* 背景图覆盖整个窗口 */
  background-position: center; /* 背景图居中 */
  background-attachment: fixed; /* 背景图固定，不随滚动条滚动 */
  margin: 0;
  padding: 0;
}
</style>
